#include <iostream>
#include <unordered_map>
using namespace std;
int dp[2 * 100001][3];
int main()
{
    int n;
    string s;
    long long ret = 0;
    cin >> n >> s;
    int sum = 0;
    for (int i = 0; i < n; ++i)
    {
        sum += s[i] - '0';
    }
    int left = sum % 3;
    dp[0][(s[0] - '0') % 3] = 1;
    for (int i = 1; i < n; ++i)
    {
        switch ((s[i] - '0') % 3)
        {
        case 0:
            dp[i][0] = dp[i - 1][0] + 1;
            dp[i][1] = dp[i - 1][1];
            dp[i][2] = dp[i - 1][2];
            break;
        case 1:
            dp[i][0] = dp[i - 1][2];
            dp[i][1] = dp[i - 1][0] + 1;
            dp[i][2] = dp[i - 1][1];
            break;
        case 2:
            dp[i][0] = dp[i - 1][1];
            dp[i][1] = dp[i - 1][2];
            dp[i][2] = dp[i - 1][0] + 1;
            break;
        }
    }
    for (int i = 0; i < n; ++i)
    {
        ret += dp[i][left];
    }
    if (left == 0)
        ret++;
    cout << ret << endl;
    return 0;
}

// 作者：uogog
// 链接：https://www.nowcoder.com/exam/test/87557781/submission?pid=60928596
// 来源：牛客网